﻿using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PLM.ConfigurationCenter.Pages {
    public partial class SettingPage : UIPage {
        public SettingPage() {
            InitializeComponent();
            InitData();
        }
        /// <summary>
        /// 
        /// </summary>
        private void InitData() {
            var m = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "RootDic").First();
            if (m != null) {
                this.uiTextBox1.Text = m.Value;
            }
            var autostart = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "AutoStart").First();
            if (autostart != null) {
                this.lb_frequency.Visible = autostart.Value.ToBoolean();
                this.cb_frequency.Visible = autostart.Value.ToBoolean();
            } else {
                this.lb_frequency.Visible = false;
                this.cb_frequency.Visible = false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_selectfolder_Click(object sender, EventArgs e) {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择服务所在文件夹";
            if (dialog.ShowDialog() == DialogResult.OK) {
                if (string.IsNullOrEmpty(dialog.SelectedPath)) {
                    MessageBox.Show(this, "文件夹路径不能为空", "提示");
                    return;
                }
                this.uiTextBox1.Text = dialog.SelectedPath;
                var m = DbManager.DbContext.Queryable<SystemSettingModel>().Where(p => p.Name == "RootDic").First();
                if (m == null) {
                    m = new SystemSettingModel {
                        Name = "RootDic",
                        Value = dialog.SelectedPath
                    };
                    DbManager.DbContext.Insertable(m).ExecuteCommand();
                } else {
                    m.Value = dialog.SelectedPath;
                    DbManager.DbContext.Updateable(m).ExecuteCommand();
                }
            }
        }

        private void uiSwitch1_ValueChanged(object sender, bool value) {
            this.lb_frequency.Visible = value;
            this.cb_frequency.Visible = value;
        }
    }
}
